package com.example.d1216;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.List;

public class ChongZhiActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView chongzibianhao;
    private TextView chikrxingminC;
    private EditText yikayue;
    private Button chongzhiM;
    private Integer kayuer;
    private String sign;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chong_zhi);
        initView();
        initData();
    }

    private void initData() {

    }

    private void initView() {
        chongzibianhao = (TextView) findViewById(R.id.chongzibianhao);
        chikrxingminC = (TextView) findViewById(R.id.chikrxingminC);
        yikayue = (EditText) findViewById(R.id.yikayueC);
        chongzhiM = (Button) findViewById(R.id.chongzhiM);
        chongzhiM.setOnClickListener(this);
        //接收Intent传入参数
        Intent intent = getIntent();
        sign = intent.getStringExtra("signC");
        //数据库查询次参数相关信息
        List<User> list = LitePal.where("kahao=?",sign).find(User.class);
        //相关信息显示到Ui
        chongzibianhao.setText(list.get(0).getKahao());
        chikrxingminC.setText(list.get(0).getKaming());
        //获取余额
        kayuer = list.get(0).getKayuer();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chongzhiM:
                //更新数据库
                String s = yikayue.getText().toString();
                User user = new User();
                user.setKayuer(kayuer+Integer.parseInt(s));
                user.updateAll("kahao = ?", sign);
                yikayue.setText("");
                Toast.makeText(ChongZhiActivity.this,"充值成功",Toast.LENGTH_SHORT).show();
                break;
        }
    }

}
